package com.amiu.shiro;

import java.util.Collection;
import java.util.Set;

import org.apache.shiro.cache.ehcache.EhCacheManager;

import org.apache.shiro.cache.CacheException;

public class ShiroEhcache<K, V> implements org.apache.shiro.cache.Cache<K, V>{
	private String DEFAULT_CACHE_NAME = "shiro_cache";
	private String cache_name;
	private org.apache.shiro.cache.Cache<K, V> cache = null;
//	private EhCacheManager cacheManager;
	private org.apache.shiro.cache.CacheManager cacheManager;
	
	public String getCache_name() {
		return cache_name;
	}

	public void setCache_name(String cache_name) {
		this.cache_name = cache_name;
		DEFAULT_CACHE_NAME = cache_name;
	}

//	public EhCacheManager getCacheManager() {
//		return cacheManager;
//	}
//
//	public void setCacheManager(EhCacheManager cacheManager) {
//		this.cacheManager = cacheManager;
//	}
	
	public org.apache.shiro.cache.CacheManager getCacheManager() {
		return cacheManager;
	}

	public void setCacheManager(
			org.apache.shiro.cache.CacheManager cacheManager) {
		this.cacheManager = cacheManager;
	}
	
	
	public org.apache.shiro.cache.Cache<K, V> getCache(){
		try {
			if(null == cache){
				cache = cacheManager.getCache(DEFAULT_CACHE_NAME);
			}
		} catch (Exception e) {
			throw new RuntimeException("找不到名为'"+DEFAULT_CACHE_NAME+"'的cache："+e);
		}
		return cache;
	}

	@Override
	public V get(K key) throws CacheException {
		return getCache().get(key);
	}
	@Override
	public V put(K key, V value) throws CacheException {
		return getCache().put(key, value);
	}
	@Override
	public V remove(K key) throws CacheException {
		return getCache().remove(key);
	}
	@Override
	public void clear() throws CacheException {
		getCache().clear();
	}
	@Override
	public int size() {
		return getCache().size();
	}
	@Override
	public Set<K> keys() {
		getCache().keys();
		return null;
	}
	@Override
	public Collection<V> values() {
		getCache().values();
		return null;
	}
	
}
